home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11247 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  86 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: HELP!!! Splitting strings
  5. Message-ID: <1996Mar22.182443.28173@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <Pine.SUN.3.91.960320094032.5117A-100000@altair.herts.ac.uk>
  11. Date: Fri, 22 Mar 1996 18:24:43 GMT
  12.  
  13. In article <Pine.SUN.3.91.960320094032.5117A-100000@altair.herts.ac.uk>, Wayne Rowley <cs2ff@herts.ac.uk> writes:
  14. >
  15. >Can anyone help me with this problem: I'm trying to split larger strings 
  16. >into a number of smaller strings, for example:
  17. >
  18. > "thisisastring" to "this" "is" "a" "string"
  19. >
  20. >I can't seem to get C to do it properly. I know that C is not a good 
  21. >language for manipulating strings, and there doesn't seem to be anything 
  22. >in string.h to help. Does anyone have any ideas?
  23.  
  24. A question I have: what is the delimiter in the source string?  If it
  25. is *you* making this determination, rather than something like a space
  26. or tab, then the problem is relatively easy to do with explicit code
  27. like in the example I show later. If you are trying to get English
  28. words to be found (without delimiters) and extracted automatically,
  29. then you have a lot of coding ahead of you (still doable in C, of
  30. course) to recognize and match patterns and words!
  31.  
  32. On the other hand, if you have delimiters, it is still relatively easy
  33. - by judicious use of the strtok() function. Of course, your comment
  34. about C not being a good language to manipulate strings is not correct,
  35. I'd say, and is likely to create a flame war, but I do not care about
  36. that.    :-)
  37.  
  38. Anyway, look at the following for a simple solution (with the simple
  39. assumption that *you* know where the string is to be broken apart and
  40. can hard-code this into the code):
  41.  
  42. zodiac{158}szh: cat foo.c
  43. #include <stdio.h>
  44. #include <string.h>
  45.  
  46. int main ()
  47. {
  48.   char src[] = "Thisisastring";
  49.   char w1[5] = "" , w2[3] = "" , w3[2] = "" , w4[7] = "";
  50.  
  51.   printf ( "Before: source = %s\nword 1 = %s\nword 2 = %s\nword 3 = %s\nword 4 = %s\n" , src , w1 , w2 , w3 , w4 );
  52.   strncpy ( w1 , src , 4 ); w1[4] = '\0';
  53.   strncpy ( w2 , src + 4 , 2 ); w2[2] = '\0';
  54.   strncpy ( w3 , src + 6 , 1 ); w3[1] = '\0';
  55.   strncpy ( w4 , src + 7 , 6 ); w4[6] = '\0';
  56.   printf ( "After : source = %s\nword 1 = %s\nword 2 = %s\nword 3 = %s\nword 4 = %s\n" , src , w1 , w2 , w3 , w4 );
  57.   return ( 0 );
  58. }
  59. zodiac{159}szh: gcc -o foo foo.c
  60. zodiac{160}szh: foo
  61. Before: source = Thisisastring
  62. word 1 = 
  63. word 2 = 
  64. word 3 = 
  65. word 4 = 
  66. After : source = Thisisastring
  67. word 1 = This
  68. word 2 = is
  69. word 3 = a
  70. word 4 = string
  71. zodiac{161}szh: 
  72.  
  73. The bottom line being that there is nothing tough about using strncpy.
  74. And this can be found in string.h, so you need to look at your library
  75. reference manual more carefully for string manipulation functions ...
  76. they *do* exist!
  77.  
  78.                                 Z
  79.  
  80.  
  81. -- 
  82. -------------------------------------------------------------------------
  83. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  84. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  85. -------------------------------------------------------------------------
  86.